<?php

namespace Fize\SBSPHP\PHP80\L02Attribute;

use ReflectionClassConstant;
use ReflectionMethod;
use ReflectionObject;

/**
 * 注解执行
 */
class MyRun
{

    public function runConst(): void
    {
        $reflection = new ReflectionClassConstant(MyClass::class, 'NAME');
        $name = $reflection->getName();
        printf("name: %s\n", $name);
        $value = $reflection->getValue();
        printf("value: %s\n", $value);
        print_r($reflection);
    }

    public function runClass(MyClass $my): void
    {
        $reflection = new ReflectionObject($my);
        $attributes = $reflection->getAttributes(MyAttributes::class);
        foreach ($attributes as $attribute) {
            $attribute->newInstance()->logClass($my);
            print "runClass -> logClass<br/>\r\n";
        }
        $my->myObject();
        print_r($attributes);
    }

    public function runMethod(MyClass $my): void
    {
        $reflection = new ReflectionMethod($my, 'myMethod');
        print_r($reflection);
        $attributes = $reflection->getAttributes(MyAttributes::class);
        foreach ($attributes as $attribute) {
            $attribute->newInstance()->logMethod($reflection->getName());
            print "runMethod -> logMethod<br/>\r\n";
        }
        $my->myMethod();
        print_r($attributes);
    }
}